package my.study.c.consul.provider.impl.app.filter;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Objects;

@Component
public class TokenFilter implements Filter {

    @Value("${feign.token}")
    String token;

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = ((HttpServletRequest) servletRequest);
        String authorization = request.getHeader("Authorization");
        if (!Objects.equals(token, authorization)) {
            throw new IOException("token 验证失败");
        }
        // 继续处理请求:
        filterChain.doFilter(servletRequest, servletResponse);
    }
}
